package com.sound
{
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;

	public class SoundPlay
	{
		private static const CYCLE_TIME:int = 10000;
		private static var _instance:SoundPlay;
		
		private var _soundCacher:Object = {};
		private var _isPlay:Boolean = true;
		private var _isEffect:Boolean = true;
		private var _currentSound:DynamicSound;
		private var _delay:Number = 1.5;
		
		public function SoundPlay()
		{
		}
		
		public static function getInstance():SoundPlay{
			if(!_instance){
				_instance = new SoundPlay;
			}
			return _instance;
		}
		
		/**
		 * 有淡入淡出效果
		 */
		public function playFade(path:String, restart:Boolean = true):void{
			if(!_isPlay){
				return;
			}
			if(_currentSound){
				stopFade();
			}
			if(restart){
				_currentSound.stopPosition = 0;
			}
			var sound:DynamicSound = _soundCacher[path] as DynamicSound;
			if(sound){
				_currentSound = sound;
				if(sound.stopPosition <= sound.length - _delay*1000 && sound.stopPosition > 0){
					sound.play(sound.stopPosition);
					sound.soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);	
				}else{
					sound.play(0, CYCLE_TIME);
				}
				sound.volume = 0;
				TweenLite.to(sound, _delay, {volume:1});
			}else{
				sound = new DynamicSound;
				sound.load(new URLRequest(path));
				sound.path = path;
				sound.addEventListener(Event.COMPLETE, loadFadeComplete);
				sound.addEventListener(IOErrorEvent.IO_ERROR, onError);
			}			
		}
		
		public function stopFade():void{
			_currentSound.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			TweenLite.to(_currentSound, _delay, {volume:0, onComplete:fadeComplete, onCompleteParams:[_currentSound]});
		}
		
		private function fadeComplete(sound:DynamicSound):void{
			sound.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			sound.stop();
		}
		/**
		 * 没有淡入淡出效果
		 */
		public function play(path:String, restart:Boolean = true):void{
			if(!_isPlay){
				return;
			}
			if(_currentSound){
				_currentSound.stop();
				_currentSound.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			}
			if(restart){
				_currentSound.stopPosition = 0;
			}
			var sound:DynamicSound = _soundCacher[path] as DynamicSound;
			if(sound){
				_currentSound = sound;
				if(sound.stopPosition < sound.length && sound.stopPosition  > 0){
					sound.play(sound.stopPosition);
					sound.soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
				}else{
					sound.play(0, CYCLE_TIME);
				}
			}else{
				sound = new DynamicSound;
				sound.load(new URLRequest(path));
				sound.path = path;
				sound.addEventListener(Event.COMPLETE, loadComplete);
				sound.addEventListener(IOErrorEvent.IO_ERROR, onError);
			}
		}
		
		private function soundComplete(event:Event):void{
			_currentSound.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			_currentSound.play(0, CYCLE_TIME);
		}
		
		public function stop():void{
			if(_currentSound){
				_currentSound.stop();
				_currentSound.soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
			}			
		}
		
		/**
		 * 特效比较短，没有关闭操作
		 */		
		public function playEffect(path:String):void{
			if(!_isEffect){
				return;
			}
			var sound:DynamicSound = _soundCacher[path] as DynamicSound;
			if(sound){
				sound.play(0);
			}else{
				sound = new DynamicSound;
				sound.load(new URLRequest(path));
				sound.addEventListener(Event.COMPLETE, loadEffectComplete);
				sound.addEventListener(IOErrorEvent.IO_ERROR, onError);
			}
		}

		private function loadFadeComplete(event:Event):void{
			var sound:DynamicSound = event.currentTarget as DynamicSound;
			sound.removeEventListener(Event.COMPLETE, loadComplete);
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_soundCacher[sound.path] = sound;
			sound.play(0, CYCLE_TIME);
			_currentSound = sound;
			_currentSound.volume = 0;
			TweenLite.to(_currentSound, _delay, {volume:1});
		}
		
		private function loadComplete(event:Event):void{
			var sound:DynamicSound = event.currentTarget as DynamicSound;
			sound.removeEventListener(Event.COMPLETE, loadComplete);
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_soundCacher[sound.path] = sound;
			sound.play(0, CYCLE_TIME);			
			_currentSound = sound;
		}
		
		private function loadEffectComplete(event:Event):void{
			var sound:DynamicSound = event.currentTarget as DynamicSound;
			sound.removeEventListener(Event.COMPLETE, loadComplete);
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_soundCacher[sound.path] = sound;
			sound.play();
		}
		
		private function onError(event:Event):void{
			var sound:DynamicSound = event.currentTarget as DynamicSound;
			sound.removeEventListener(Event.COMPLETE, loadComplete);
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onError);
		}
	}
}